package edu.unf.cen6940.web;

import java.text.SimpleDateFormat;
import java.sql.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.hibernate.stat.CategorizedStatistics;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import edu.unf.cen6940.domain.Category;
import edu.unf.cen6940.domain.Faculty;
import edu.unf.cen6940.domain.Facultyresearch;
import edu.unf.cen6940.domain.Generatedproduct;
import edu.unf.cen6940.domain.Researchkeyword;
import edu.unf.cen6940.domain.Student;
import edu.unf.cen6940.domain.Thesiscommittee;
import edu.unf.cen6940.domain.Thesistool;
import edu.unf.cen6940.dao.CategoryDAO;
import edu.unf.cen6940.dao.FacultyDAO;
import edu.unf.cen6940.dao.FacultyresearchDAO;
import edu.unf.cen6940.dao.GeneratedproductDAO;
import edu.unf.cen6940.dao.ResearchkeywordDAO;
import edu.unf.cen6940.dao.StudentDAO;
import edu.unf.cen6940.dao.ThesiscommitteeDAO;
import edu.unf.cen6940.dao.ThesistoolDAO;
import edu.unf.cen6940.domain.Thesis;
import edu.unf.cen6940.dao.ThesisDAO;
import edu.unf.cen6940.service.StudentService;

@Controller
public class RMSController {
	@Autowired
	ThesisDAO th = null;
	
	@Autowired
	FacultyresearchDAO frh = null;
	
	@Autowired
	StudentDAO studentDAO;
	
	@Autowired
	CategoryDAO categoryDAO;
	
	@Autowired
	ThesistoolDAO thesistoolDAO;
	
	@Autowired
	ThesiscommitteeDAO thesiscommitteeDAO;
	
	@Autowired
	GeneratedproductDAO generatedProductDAO;
	
	@Autowired
	FacultyDAO facultyDAO;
	
	@Autowired
	ResearchkeywordDAO researchkeywordDAO;
	
	@RequestMapping(value="/index", method=RequestMethod.GET)
	public void index(Model model){
		Set<Thesis> thesisList = th.findAllThesiss((th.getCount() - 5), 5);
		
		Set<Category> categoryList = categoryDAO.findRandomCategorys(0, 5);
		//Set<Facultyresearch> researchList = frh.findAllFacultyresearchs();
		
		model.addAttribute("thesisList", thesisList);
		model.addAttribute("categoryList", categoryList);
		//model.addAttribute("researchList",researchList);
	}
	
	@RequestMapping(value="/search", method=RequestMethod.POST)
	public String search(@RequestParam(required=true) String searchTerm, @RequestParam(required=false, value="thesis") String thesis, @RequestParam(required=false, value="research") String research, Model model){
		String rtn = null;
		
		if(research == null)
			research = "";
		if(thesis == null)
			thesis = "";
		if(research.equals("yes") && thesis.equals("yes"))
		{
			rtn = "redirect:/bothsearch?searchTerm="+searchTerm;
		}
		else if(thesis.equals("yes")){
			//rtn = "redirect:/thesis/search.html?searchTerm="+searchTerm;
			rtn = "redirect:/searchThesis?searchTerm="+searchTerm;
		}else if(research.equals("yes")){
			//rtn = "redirect:/research/search.html?searchTerm="+searchTerm;
			rtn = "redirect:/searchFacultyresearch?searchTerm="+searchTerm;
		}
		else
		{
			rtn = "redirect:/index";
		}
		
		return rtn;
	}
	
	@RequestMapping("/bothsearch")
	public String bothsearch(@RequestParam(required=true) String searchTerm,Model model){
				model.addAttribute("thesiss",th.searchByTerm(searchTerm));
				model.addAttribute("facultyresearchs", frh.searchByTerm(searchTerm));
				model.addAttribute("searchTerm", searchTerm);
		return "dualList";
	}
	
	@RequestMapping("/advancedThesisSearch")
	public String advancedThesisSearch(Model model){
		model.addAttribute("thesis", new Thesis());
		Set<Student> studentData = studentDAO.findAllStudents();
		Map<Integer,String> studentMap = new HashMap<Integer, String>();
		for(Student student:studentData){
			studentMap.put(student.getStudentId(),student.getFirstName()+" "+student.getLastName());
		}
		model.addAttribute("students", studentMap);
		Set<Category> categorySet = categoryDAO.findAllCategorys();
		Map<Integer,String> categoryData = new HashMap<Integer, String>();
		for(Category category:categorySet){
			categoryData.put(category.getCategoryId(),category.getCategoryDescription());
		}
		model.addAttribute("categories", categoryData);
		Set<Thesistool> thesistoolSet = thesistoolDAO.findAllThesistools();
		Map<Integer,String> thesistoolData = new HashMap<Integer, String>();
		for(Thesistool thesistool:thesistoolSet){
			thesistoolData.put(thesistool.getThesisToolId(),thesistool.getToolDescription());
		}
		model.addAttribute("thesistoolss", thesistoolData);
		Set<Faculty> facultyData = facultyDAO.findAllFacultys();
		Map<Integer,String> facultyMap = new HashMap<Integer, String>();
		for(Faculty faculty:facultyData){
			facultyMap.put(faculty.getFacultyId(),faculty.getFirstName()+" "+faculty.getLastName());
		}
		model.addAttribute("facultyList",facultyMap);
		Set<Thesiscommittee> thesiscommitteeSet = thesiscommitteeDAO.findAllThesiscommittees();
		Map<Integer,String> thesiscommitteeData = new HashMap<Integer, String>();
		for(Thesiscommittee thesiscommittee:thesiscommitteeSet){
			thesiscommitteeData.put(thesiscommittee.getThesisCommitteeId(),
					thesiscommittee.getFacultyByChair().getLastName()+"| "+thesiscommittee.getFacultyByChair().getFirstName()
					/*+((thesiscommittee.getFacultyByCommitteeMember1() != null)?" "+thesiscommittee.getFacultyByCommitteeMember1().getLastName()+"| "+thesiscommittee.getFacultyByCommitteeMember1().getFirstName():"")
					+((thesiscommittee.getFacultyByCommitteeMember2() != null)?" "+thesiscommittee.getFacultyByCommitteeMember2().getLastName()+"| "+thesiscommittee.getFacultyByCommitteeMember2().getFirstName():"")
					+((thesiscommittee.getFacultyByCommitteeMember3() != null)?" "+thesiscommittee.getFacultyByCommitteeMember3().getLastName()+"| "+thesiscommittee.getFacultyByCommitteeMember3().getFirstName():"")
					+((thesiscommittee.getFacultyByCommitteeMember4() != null)?" "+thesiscommittee.getFacultyByCommitteeMember4().getLastName()+"| "+thesiscommittee.getFacultyByCommitteeMember4().getFirstName():"")
					+((thesiscommittee.getFacultyByCommitteeMember5() != null)?" "+thesiscommittee.getFacultyByCommitteeMember5().getLastName()+"| "+thesiscommittee.getFacultyByCommitteeMember5().getFirstName():"")*/
					);
		}
		model.addAttribute("thesiscommitteess", thesiscommitteeData);
		Set<Generatedproduct> generatedproductSet = generatedProductDAO.findAllGeneratedproducts();
		Map<Integer,String> generatedproductData = new HashMap<Integer, String>();
		for(Generatedproduct generatedproduct:generatedproductSet){
			generatedproductData.put(generatedproduct.getGeneratedProductId(),generatedproduct.getProductName());
		}
		model.addAttribute("generatedproductss", generatedproductData);
		
		return "advancedThesisSearch";
	}
	
	@RequestMapping("/advancedResearchSearch")
	public String advancedResearchSearch(Model model){
		model.addAttribute("facultyresearch", new Facultyresearch());
		Set<Faculty> facultyData = facultyDAO.findAllFacultys();
		Map<Integer,String> facultyMap = new HashMap<Integer, String>();
		for(Faculty faculty:facultyData){
			facultyMap.put(faculty.getFacultyId(),faculty.getFirstName()+" "+faculty.getLastName());
		}
		model.addAttribute("facultyList",facultyMap);
		Set<Researchkeyword> researchkeywordData = researchkeywordDAO.findAllResearchkeywords();
		Map<Integer,String> researchkeywordMap = new HashMap<Integer, String>();
		for(Researchkeyword researchkeyword:researchkeywordData){
			researchkeywordMap.put(researchkeyword.getResearchKeywordId(),researchkeyword.getKeyword());
		}
		model.addAttribute("researchkeywordList",researchkeywordMap);
		Set<Category> categorySet = categoryDAO.findAllCategorys();
		Map<Integer,String> categoryData = new HashMap<Integer, String>();
		for(Category category:categorySet){
			categoryData.put(category.getCategoryId(),category.getCategoryDescription());
		}
		model.addAttribute("categoryList", categoryData);
		return "advancedResearchSearch";
	}
	
	@RequestMapping("submitAdvancedSearchThesis")
	public String submitAdvancedSearchThesis(@ModelAttribute Thesis thesis, Model model, @RequestParam(required = false) String endDate){
		Set<Thesis> thesiss = null;
		if(endDate == null || endDate == ""){
			thesiss = th.advancedSearch(thesis);
		}else{
			try{
				Date end = new Date( new SimpleDateFormat("MM/dd/yyyy").parse(endDate).getTime() );
				thesiss = th.advancedSearch(thesis,end);
			}catch (Exception e) {
				System.out.println("error thrown");
			}
		}
		model.addAttribute("thesiss",thesiss);
		return "thesis/listThesiss";
	}
	
	@RequestMapping("submitAdvancedSearchResearch") 
	public String submitAdvancedSearchResearch(@ModelAttribute Facultyresearch facultyresearch, Model model){
		model.addAttribute("facultyresearchs", frh.advancedSearch(facultyresearch));
		return "facultyresearch/listFacultyresearchs";
	}
	
	/**
	 * Register custom, context-specific property editors
	 * 
	 */
	@InitBinder
	public void initBinder(WebDataBinder binder, HttpServletRequest request) { // Register static property editors.
		binder.registerCustomEditor(java.util.Calendar.class, new org.skyway.spring.util.databinding.CustomCalendarEditor());
		
		SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
		dateFormat.setLenient(false);
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
	}
}
